<template>
  <div>
    <validator name='v'>
      <form novalidate  enctype="multipart/form-data">
        <div class="container-fluid table-responsive">
          <table class="table-noborder">
              <tbody>
                  <tr>
                      <td width="15%" align="right"><label class="control-label">上一级：</label></td>
                      <td colspan="5"><input type="text" class="form-control" v-model="parentname" placeholder="请输入名字" disabled></td>
                  </tr>
                  <tr>
                      <td rowspan="4" align="right"><label class="control-label">头像：</label></td>
                      <td width="23%" rowspan="4">
                        <!-- <img src="" width="150" height="150" alt=""/> -->
                        {{{ img }}}
                          <input type="file" id="fileiput" v-model="model.touxiang"></td>
                      <td width="13%" align="right"><label class="control-label">姓名：</label></td>
                      <td width="20%"><input type="text" class="form-control" placeholder="请输入名字" v-model="model.name"></td>
                      <td width="13%" align="right"><label class="control-label">登录名：</label></td>
                      <td width="21%"><input type="text" class="form-control" placeholder="请输入名字" v-model="model.ename"></td>
                  </tr>
                  <tr>
                    <td align="right"><label class="control-label">所属分公司：</label></td>
                    <td style="align:left">
                      <v-select :value.sync="model.f_fengongsi"
                          :options='selects' placeholder='请选择'
                          close-on-select clear-button
                          ></v-select>
                    </td>
                    <td width="13%" align="right"><label class="control-label">所属分公司编号：</label></td>
                    <td style="align:left">
                      <v-select :value.sync="model.f_fengongsinum"
                          :options='selects' placeholder='请选择'
                          close-on-select clear-button
                          ></v-select>
                    </td>
                  </tr>
                  <tr>
                    <td align="right"><label class="control-label">分公司发票台头：</label></td>
                    <td style="align:left">
                      <v-select :value.sync="model.f_fapiaotaitu"
                          :options='selects' placeholder='请选择'
                          close-on-select clear-button
                          ></v-select>
                    </td>
                    <td width="13%" align="right"><label class="control-label">分公司发票税号：</label></td>
                    <td style="align:left">
                      <v-select :value.sync="model.f_fgsshuihao"
                          :options='selects' placeholder='请选择'
                          close-on-select clear-button
                          ></v-select>
                    </td>
                  </tr>
                  <tr>
                    <td align="right"><label class="control-label">分公司发票地址：</label></td>
                    <td style="align:left">
                      <v-select :value.sync="model.f_fgsdizhi"
                          :options='selects' placeholder='请选择'
                          close-on-select clear-button
                          ></v-select>
                    </td>
                    <td width="13%" align="right"><label class="control-label">分公司发票电话：</label></td>
                    <td style="align:left">
                      <v-select :value.sync="model.f_fgsdianhua"
                          :options='selects' placeholder='请选择'
                          close-on-select clear-button
                          ></v-select>
                    </td>
                  </tr>
                  <tr>
                      <td align="right"><label class="control-label">工号：</label></td>
                      <td><input type="text" class="form-control" placeholder="请输入名字" v-model="model.gonghao"></td>
                      <td align="right"><label class="control-label">发票号：</label></td>
                      <td><input type="text" class="form-control" placeholder="请输入名字" v-model="model.f_invoicenum"></td>
                      <td align="right"><label class="control-label">主界面：</label></td>
                      <td style="align:left">
                        <v-select :value.sync="model.operpage"
                            :options='selects' placeholder='请选择'
                            close-on-select clear-button
                            ></v-select>
                      </td>
                  </tr>
                  <tr>
                      <td align="right"><label class="control-label">银行名称：</label></td>
                      <td style="align:left">
                        <v-select :value.sync="model.f_bankname"
                            :options='selects' placeholder='请选择'
                            close-on-select clear-button
                            ></v-select>
                      </td>
                      <td align="right"><label class="control-label">最大单笔售气量：</label></td>
                      <td><input type="text" class="form-control" placeholder="请输入名字" v-model="model.maxgassellnum"></td>
                      <td align="right"><label class="control-label">民用可修改限购上限：</label></td>
                      <td><input type="text" class="form-control" placeholder="请输入名字" v-model="model.myxgmax"></td>
                  </tr>
                  <tr>
                    <td align="right"><label class="control-label">混合型可修改限购上限：</label></td>
                    <td><input type="text" class="form-control" placeholder="请输入名字" v-model="model.hemax"></td>
                    <td align="right"><label class="control-label">非民用可修改限购上限：</label></td>
                    <td><input type="text" class="form-control" placeholder="请输入名字" v-model="model.gyxgmax"></td>
                    <td align="right">&nbsp;</td>
                    <td>&nbsp;</td>
                  </tr>
                  <tr>
                    <td align="right"><label class="control-label">描述：</label></td>
                    <td colspan="5">
                    <textarea class="form-control" v-model="model.f_description"></textarea>
                    </td>
                  </tr>
                  <tr>
                    <td align="right">&nbsp;</td>
                    <td colspan="2">
                      <button @click="confirm()" type="button" class="btn btn-success btn-md">
                        <span class="glyphicon glyphicon-floppy-disk"></span> 保存
                      </button>
                      <button type="button" class="btn btn-danger btn-md">
                        <span class="glyphicon glyphicon-remove"></span> 取消
                      </button>
                      <button type="button" class="btn btn-default btn-md" @click="$back()">
                        <span class="glyphicon glyphicon-log-out"></span> 返回
                      </button>
                    </td>
                  </tr>
              </tbody>
          </table>
      </div>
      </form>
    </validator>
  </div>
</template>

<script>
import co from 'co'

let saveGen = function * (self) {
  self.deliver.f_userid = self.model.f_userid
  self.deliver.lastinputdate = self.model.lastinputdate
  self.model.f_fengongsi = self.model.f_fengongsi[0]
  self.model.f_fengongsinum = self.model.f_fengongsinum[0]
  self.model.f_fapiaotaitu = self.model.f_fapiaotaitu[0]
  self.model.f_fgsshuihao = self.model.f_fgsshuihao[0]
  self.model.f_fgsdizhi = self.model.f_fgsdizhi[0]
  self.model.f_fgsdianhua = self.model.f_fgsdianhua[0]
  self.model.operpage = self.model.operpage[0]
  self.model.f_bankname = self.model.f_bankname[0]
  //yield self.$http.post('/rs/logic/errorhandle', {data: self.deliver})
  //let res = yield self.$http.post('rs/data/save', {data: {ids: self.userids, roleid: self.data.id}})
  console.log(self.model)
  let msg = yield self.$http.post('rs/data/save',{data:self.model})
   // 通知项目保存成功。
  yield self.$showMessage(msg.bodyText)
  self.$back('refresh')
}
let readFile = function (self) {
  console.log(self)
  var fileiput = document.getElementById('fileiput')
  var file = fileiput.files[0]
  if (!/image\/\w+/.test(file.type)) {
      // alert ("请确保文件为图像类型")
    return false
  }
  var reader = new FileReader()
  reader.readAsDataURL(file)
  reader.onload = function (e) {
    self.img = 'this.result'
    self.img = '<img src="' + this.result + '" alt="" height="150" width="150"/>'
  }
}
export default {
  title: '项目编写',
  props: ['parent','data'],
  data () {
    return {
      model: {
        parentid:'',
        id:'',
        name:'',
        ename:'',
        touxiang: '',
        f_fengongsi:'',
        f_fengongsinum:'',
        f_fapiaotaitu:'',
        f_fgsshuihao:'',
        f_fgsdizhi:'',
        f_fgsdianhua:'',
        gonghao:'',
        f_invoicenum:'',
        operpage:'',
        f_bankname:'',
        maxgassellnum:'',
        myxgmax:'',
        hemax:'',
        gyxgmax:'',
        f_description:'',
        resourcetype : "user"
      },
      deliver: {}, // 业务逻辑需要的参数
      img: '',
      parentname:''
    }
  },
  methods: {
    confirm () {
      let gen = saveGen(this)
      return co(gen)
    }
    // readFile () {
    // }
  },
  computed: {
    selects () {
      return [
        {label: '', value: ''},
        {label: '湖北和芯科技', value: '湖北和芯科技'},
        {label: '奥枫科技', value: '奥枫科技'}
      ]
    }
  },
  ready () {
    console.log('FileReader type is ' + typeof FileReader)
    let that = this
    var input = document.getElementById('fileiput')
    if (typeof FileReader === 'undefined') {
      that.img = '抱歉，你的浏览器不支持 FileReader'
      input.setAttribute('disabled', 'disabled')
    } else {
      input.addEventListener('change', function () { readFile(that) }, false)
    }
    //将要修改的值设置默认值
    if(this.parent){
      this.model.parentid = this.parent.id
      this.parentname = this.parent.name
      this.model = Object.assign({}, this.model, this.data)
    }else{
      alert("lalala")

    }
  }
}

</script>
